package com.feng.educms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feng.commonutils.R;
import com.feng.educms.entity.CrmBanner;
import com.feng.educms.mapper.CrmBannerMapper;
import com.feng.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.eduservice.dto.bannerQuery;
import com.feng.eduservice.entity.EduTeacher;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 * 首页banner表 服务实现类
 * </p>
 *
 * @author fengge
 * @since 2023-03-24
 */
@Service
public class CrmBannerServiceImpl extends ServiceImpl<CrmBannerMapper, CrmBanner> implements CrmBannerService {

    @Override
    public R pageBanner(long page, long limit, bannerQuery bannerquery) {
        LambdaQueryWrapper<CrmBanner> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(bannerquery.getTitle()),CrmBanner::getTitle,bannerquery.getTitle());
        queryWrapper.ge(!StringUtils.isEmpty(bannerquery.getBegin()), CrmBanner::getGmtCreate,bannerquery.getBegin());
        queryWrapper.le(!StringUtils.isEmpty(bannerquery.getEnd()),CrmBanner::getGmtCreate,bannerquery.getEnd());
        queryWrapper.orderByDesc(CrmBanner::getGmtCreate) ;
        Page<CrmBanner> pageBanner=new Page<>(page,limit);
        this.page(pageBanner,queryWrapper);
        long total = pageBanner.getTotal();
        List<CrmBanner> records = pageBanner.getRecords();
        return R.ok().data("items",records).data("total",total);
    }

    @Override
    @Cacheable(key="'selectIndexList'",value = "banner")
    public List<CrmBanner> getAllBanner() {
        LambdaQueryWrapper<CrmBanner> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(CrmBanner::getId);
        queryWrapper.last("limit 2");
         List<CrmBanner> list = baseMapper.selectList(null);
        return list;
    }
}
